import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Deduction } from './deduction.entity';
import { Repository } from 'typeorm';
import { UserSys } from 'src/user/userSys.entity';
import { Project } from 'src/project/project.entity';
import { NoticeService } from 'src/notice/notice.service';

@Injectable()
export class DeductionService {
    constructor(
        @InjectRepository(Deduction)
        public readonly deduction: Repository<Deduction>,
        private readonly noticeService: NoticeService
    ) { }

    public async create(
        user: UserSys[],
        target: UserSys,
        project: Project,
        data: any
    ) {
        const entity = this.deduction.create({
            ...data,
            user,
            target,
            project,
            system: target.system
        })
        for (const item of user) {
            this.noticeService.create(
                item,
                target,
                "您有一条扣罚消息",
                `项目 ${project.name}
                原因:
                ${data.reason}
                扣除:${data.score} 分`
            )
        }

        return await this.deduction.save(entity);
    }
}
